/**
 * Create By FengZheng
 * On 2019/9/1 0:54
 */

/**
 * 查找当天课程信息
 */
(function ($) {
    "use strict";

    let $dom = {
        $classContainer: $("#class-container"),
        $clockContainer: $("#clock-container")
    };

    let $system = {
        $classArray: []
    };


    /**
     * 查找当天课程信息
     */
    function queryCurrentClassInfo() {
        $.ajax({
            url: "/parents/frame/current-class-info",
            type: "post",
            dataType: "json",
            success: function (resultD) {
                if( (typeof resultD)==="object" ){
                    let status = resultD.status;
                    if(status === 200){
                        let info = resultD.info;

                        if( (typeof info)==="string" ){
                            info = JSON.parse(info);
                        }

                        for(let i=0,len=info.length; i<len; i++){
                            // 学生真实姓名判断
                            let studentName = info[i].studentName;
                            if(info[i].studentRealName !== null && info[i].studentRealName !== ""){
                                studentName = info[i].studentRealName;
                            }

                            // 上课时间
                            let classTime = info[i].classTime;

                            let classData = {
                                "studentName": studentName,
                                "classTime": classTime,
                                "classId": info[i].classId
                            };

                            $system.$classArray.push(classData);
                        }

                        // 开始输出
                        doOutput();
                    }
                }
            }
        });
    }

    /**
     * 遍历输出信息
     */
    function doOutput() {
        if($system.$classArray.length !== 0){
            let timeStamp = $dom.$clockContainer.attr("data-time").trim();

            for(let i=0,len=$system.$classArray.length; i<len; i++){
                let cInfo = $system.$classArray[i],
                    classTime = cInfo.classTime,
                    hasClass = false,
                    timeContent = "";

                // 还未开始上课
                if(classTime > timeStamp){
                    hasClass = true;
                    timeContent = (Math.round((classTime - timeStamp)/60000)) + "分钟后"
                }else {
                    let betweenTime = timeStamp - classTime;
                    if(betweenTime < 720000){
                        hasClass = true;
                        timeContent = "已经";
                    }
                }

                let content = "";
                if(hasClass){
                    content = "<div class=\"row\">"+
                        "<div class=\"explain-txt\">"+ cInfo.studentName +"同学的课程"+ timeContent +"开始</div>"+
                        "</div>"+
                        "<div class=\"row\">"+
                        "<button data-id='"+ cInfo.classId +"' name=\"join-class\" type=\"button\" class=\"btn btn-default explain-txt\">进入教室</button>"+
                        "</div><br/>";
                    $dom.$classContainer.append(content);
                    bindClickEvent();
                }else{
                    $dom.$classContainer.empty();
                    let content = "<div class=\"row\">"+
                        "<div class=\"explain-txt\">今日之内暂无课程信息</div>"+
                        "</div>";
                    $dom.$classContainer.append(content);
                }
            }
        }else{
            let content = "<div class=\"row\">"+
                "<div class=\"explain-txt\">今日之内暂无课程信息</div>"+
                "</div>";
            $dom.$classContainer.append(content);
        }
    }

    /**
     * 绑定事件
     */
    function bindClickEvent() {
        $("[name='join-class']").on("click",function (ev) {
            let event = ev || window.event;
            let target = event.target || event.srcElement;

            let classId = $(target).attr("data-id");

            let mWindow = window.open("","_blank","width=500,height=500");
            mWindow.document.write("ID:"+classId);
        });
    }

    $(document).ready(function () {
        queryCurrentClassInfo();
    });
})(window.jQuery);

/**
 * 时钟控制
 */
(function ($) {
    "use strict";

    /**
     * DOM元素
     */
    let $dom = {
        $clockContainer: $("#clock-container"),
        $clockHour: $("#clock-hour"),
        $clockMinute: $("#clock-minute"),
        $clockSecond: $("#clock-second")
    };

    /**
     * 时间
     */
    let $time = {
        $hours: 0,
        $minutes: 0,
        $seconds: 0
    };

    /**
     * 指针角度
     */
    let pointAngle = {
        hours: 0,
        minutes: 0,
        second: 0
    };

    /**
     * 时间初始化
     */
    function initTime() {
        let timeStamp = $dom.$clockContainer.attr("data-time").trim();

        if( timeStamp !== undefined){
            timeStamp = parseInt(timeStamp);

            if(timeStamp > 0){
                let date = new Date(timeStamp);

                $time.$hours = date.getHours();
                $time.$minutes = date.getMinutes();
                $time.$seconds = date.getSeconds();
            }
        }
    }

    /**
     * 初始化指针角度
     */
    function initAngle(){

        let miniHour = $time.$hours;
        if($time.$hours > 12){
            miniHour = $time.$hours - 12;
        }

        pointAngle.hours = miniHour * 30 + ($time.$minutes * 60 + $time.$seconds)* 0.008334;
        pointAngle.minutes = $time.$minutes * 0.1 + ($time.$minutes * 60) * 0.1;
        pointAngle.second = $time.$seconds * 6;

        pointReset();
    }

    /**
     * 指针角度复原
     */
    function pointReset() {
        pointAngle.hours = (pointAngle.hours >= 360) ? (pointAngle.hours - 360) : pointAngle.hours;
        pointAngle.minutes = (pointAngle.minutes >= 360) ? (pointAngle.minutes - 360) : pointAngle.minutes;
        pointAngle.second = (pointAngle.second >= 360) ? (pointAngle.second - 360) : pointAngle.second;
    }

    /**
     * 指针旋转
     */
    function pointRotate(){
        $dom.$clockHour.css("-webkit-transform","translate(-50%, -100%) rotate("+pointAngle.hours+"deg)").css("transform","translate(-50%, -100%) rotate("+pointAngle.hours+"deg");
        $dom.$clockMinute.css("-webkit-transform","translate(-50%, -100%) rotate("+pointAngle.minutes+"deg)").css("transform","translate(-50%, -100%) rotate("+pointAngle.minutes+"deg");
        $dom.$clockSecond.css("-webkit-transform","translate(-50%, -100%) rotate("+pointAngle.second+"deg)").css("transform","translate(-50%, -100%) rotate("+pointAngle.second+"deg");

        pointAngle.hours += 0.008334;
        pointAngle.minutes += 0.1;
        pointAngle.second += 6;

        pointReset();
    }

    $(document).ready(function () {
        initTime();
        initAngle();
        pointRotate();

        window.setInterval(pointRotate,1000);
    });
}(window.jQuery));